
prog drop _all
macro drop _all
version 11

cap program drop names_clean_small

program define AreaCompetition
	syntax varlist(max = 7)

local input_names `varlist'


tokenize `varlist'
local space `1'
local firm `2'
local land_sqft `3'
local Quant `4'
local instyear `5'
local instmonth `6'
local instday `7'

egen Mon = group(`instyear' `instmonth')

tempfile all operatorspacemerge spacemerge

tostring `space',replace

********************************************************************************

bysort `space' : egen `space'_area = sum(`land_sqft')
bysort `space' `firm' : egen `space'_area_firm = sum(`land_sqft')

gen counter = 1

* Number of leases sigend by space and firm each day
bysort `firm' `space' `instyear' `instmonth' `instday' : egen leasecount = sum(counter)
bysort `firm' `space' `instyear' `instmonth' `instday' : egen landtotal  = sum(land_sqft)

* Number of leases signed by space each day
bysort `space' `instyear' `instmonth' `instday' : egen total_count = sum(counter)
bysort `space' `instyear' `instmonth' `instday' : egen total_sqft  = sum(land_sqft)

sort `space' `firm'
save `all',replace

********************************************************************************
********************************************************************************
********************************************************************************
* Absolute share of leases signed by firm in space

keep `firm' `space'
drop if `space' == ""

gen counter = 1
bysort `firm' `space': egen firm_space_count = sum(counter)
label variable firm_space_count `"Count leases signed by firm (`space')"'

bysort `space' : egen space_count = sum(counter)
gen firmshare = firm_space_count/space_count
label variable firmshare `"Share leases signed by firm (`space')"'

drop counter firm_space_count space_count
duplicates drop

sort `space' `firm'
merge `space' `firm' using `all'
tab _merge
drop _merge

sort `space' `firm' `instyear' `instmonth' `instday' `Quant' 

save `all', replace

**********************************
* Cumulative share of leases signed by firm across time in space

keep `firm' `space' `instyear' `instmonth' `instday' leasecount landtotal `Quant' `space'_area

drop if `space' == ""
drop if `instyear' == .

duplicates drop
duplicates drop `space' `firm' `instyear' `instmonth' `instday' `Quant', force

sort `space' `firm' `instyear' `instmonth' `instday' `Quant'
by `space' `firm' `instyear' `instmonth' `instday': gen dupcount = _n

sort `firm' `space' `instyear' `instmonth' `instday'
by `firm' `space' : gen firm_space_count_cum = sum(leasecount) if dupcount == 1
label variable firm_space_count_cum `"Cum. leases signed by firm (`space')"'

sort `firm' `space' `instyear' `instmonth' `instday'
by `firm' `space': gen firm_space_sqft_cum = sum(landtotal) if dupcount == 1
label variable firm_space_sqft_cum `"Cum. land signed by firm (`space')"'

sort `space' `firm' `Quant' `instyear' `instmonth' `instday'
by `space' `firm' `Quant': gen rank = _n

foreach var of varlist firm_space_count_cum firm_space_sqft_cum {
	sort `space' `firm' `instyear' `instmonth' `instday' `var'
	by `space' `firm' `instyear' `instmonth' `instday' : carryforward `var',replace
}

gen firm_space_sqft_remain = `space'_area if rank == 1
label variable firm_space_sqft_remain "Area remaining by wellpad and firm"

sort `space' `firm' `instyear' `instmonth' `instday' 
by `space' `firm': replace firm_space_sqft_remain = `space'_area - firm_space_sqft_cum[_n-1] if `space' != "" & `space' == `space'[_n-1] & `firm' == `firm'[_n-1] & rank != 1

sort `firm' `space' `Quant' `instyear' `instmonth' `instday'
by `firm' `space' `Quant' : gen opcount = _n

by `firm' `space' `Quant' : gen firm_space_cumTime = firm_space_count_cum if opcount == 1
sort `firm' `space' `Quant' opcount
by `firm' `space' `Quant' : carryforward firm_space_cumTime, replace
label variable firm_space_cumTime `"Cum. leases signed by firm at begin `Quant' (`space')"'

by `firm' `space' `Quant' : gen firm_space_sqft_cumTime = firm_space_sqft_cum if opcount == 1
sort `firm' `space' `Quant' opcount
by `firm' `space' `Quant' : carryforward firm_space_sqft_cumTime, replace
label variable firm_space_sqft_cumTime `"Cum. land signed by firm at begin `Quant' (`space')"'

drop opcount
gsort `firm' `space' `Quant' -`instyear' -`instmonth' -`instday'
by `firm' `space' `Quant' : gen opcount = _n
by `firm' `space' `Quant' : gen firm_space_sqft_remainTime = firm_space_sqft_remain if opcount == 1
sort `firm' `space' `Quant' opcount
by `firm' `space' `Quant' : carryforward firm_space_sqft_remainTime, replace

drop dupcount rank opcount

sort `space' `firm' `Quant'
save `operatorspacemerge',replace

keep `Quant' `space' `firm' *Time `space'_area
duplicates drop
sort `space' `firm' `Quant'
by `space' `firm': gen temp = firm_space_sqft_remainTime[_n-1]
replace temp = `space'_area if `Quant' == 1
sort `space' `firm' `Quant'
merge `space' `firm' `Quant' using `operatorspacemerge', update
tab _merge
drop _merge
drop firm_space_sqft_remainTime
rename temp firm_space_sqft_remainTime

sort `space' `firm' `instyear' `instmonth' `instday' `Quant'
save `operatorspacemerge',replace

**********************************

clear
use `all'

keep total_count total_sqft `space' `instyear' `instmonth' `instday' `space'_area

drop if `space' == ""
drop if `instyear' == .

duplicates drop `space' `instyear' `instmonth' `instday', force

sort `space' `instyear' `instmonth' `instday'
by `space' : gen total_space_count_cum = sum(total_count)
label variable total_space_count_cum `"Cum. leases signed (`space')"'

sort `space' `instyear' `instmonth' `instday'
by `space' : gen total_space_sqft_cum = sum(total_sqft)
label variable total_space_sqft_cum `"Cum. land signed (`space')"'

sort `space' `instyear' `instmonth' `instday'
by `space' : gen total_space_sqft_remain = `space'_area - total_space_sqft_cum[_n-1] if `space' != "" & `space' == `space'[_n-1]
replace total_space_sqft_remain = `space'_area if total_space_sqft_remain == . & `space' != `space'[_n-1]
label variable total_space_sqft_remain "Area remaining by wellpad and firm"

duplicates drop
sort `space' `instyear' `instmonth' `instday'
save `spacemerge',replace

**********************************

clear
use `operatorspacemerge'
sort `space' `instyear' `instmonth' `instday'
merge `space' `instyear' `instmonth' `instday' using `spacemerge', update
tab _merge
drop _merge

sort `space' `firm' `instyear' `instmonth' `instday' `Quant'
merge `space' `firm' `instyear' `instmonth' `instday' `Quant' using `all', update
tab _merge
*drop if _merge == 2
drop _merge

foreach var of varlist *`Quant' {
	sort `space' `firm' `Quant' `var'
	by `space' `firm' `Quant' : carryforward `var',replace
}
duplicates drop

**********************************

sort `space' `Quant' `instyear' `instmonth' `instday'

by `space' `Quant' : gen opcount = _n
by `space' `Quant' : gen total_space_cumTime = total_space_count_cum if opcount == 1
sort `space' `Quant' opcount
by `space' `Quant' : carryforward total_space_cumTime, replace
label variable total_space_cumTime `"Cum. leases signed begin `Quant' (`space')"'

by `space' `Quant' : gen total_space_sqft_cumTime = total_space_sqft_cum if opcount == 1
sort `space' `Quant' opcount
by `space' `Quant' : carryforward total_space_sqft_cumTime, replace
label variable total_space_sqft_cumTime `"Cum. land signed begin `Quant' (`space')"'

drop opcount
gsort `space' `Quant' -`instyear' -`instmonth' -`instday'

by `space' `Quant' : gen opcount = _n
by `space' `Quant' : gen total_space_sqft_remainTime = total_space_sqft_remain if opcount == 1
sort `space' `Quant' opcount
by `space' `Quant' : carryforward total_space_sqft_remainTime, replace

drop opcount

sort `space' `Quant'
save `spacemerge',replace

keep `Quant' `space' *Time `space'_area
drop firm_*
duplicates drop
sort `space' `Quant'
by `space': gen temp = total_space_sqft_remainTime[_n-1]
replace temp = `space'_area if `Quant' == 1
keep `Quant' `space' temp
duplicates drop
sort `space' `Quant'
merge `space' `Quant' using `spacemerge'
tab _merge
drop _merge
drop total_space_sqft_remainTime
rename temp total_space_sqft_remainTime
duplicates drop

sort `space' `firm' `instyear' `instmonth' `instday' `Quant'
save `all', replace

**********************************

keep `space' `Quant' `instyear' `instmonth' `instday' `firm' leasecount total_count firm_space_count_cum total_space_count_cum
duplicates drop

* Share of leases signed by a firm in any period and space
gen firm_space_share_time = leasecount/total_count
label variable firm_space_share_time `"Firm share by period (`space')"'

* Cumulative share of leases signed by a firm in any period and space
gen firm_space_share_time_cum = firm_space_count_cum/total_space_count_cum
label variable firm_space_share_time_cum `"Firm cum. share (`space')"'

sort `firm' `space' `Quant' `instyear' `instmonth' `instday'
by `firm' `space' `Quant' : gen opcount = _n
by `firm' `space' `Quant' : gen firm_space_share_time_cumTime = firm_space_share_time_cum if opcount == 1
sort `firm' `space' `Quant' opcount
by `firm' `space' `Quant' : carryforward firm_space_share_time_cumTime , replace

drop opcount

sort `space' `firm' `instyear' `instmonth' `instday' `Quant' 
merge `space' `firm' `instyear' `instmonth' `instday' `Quant' using `all'
tab _merge
drop _merge
sort `space'
save `all', replace

**********************************
* Space-level HHI

drop if `space' == ""
drop if instyear == .

keep `firm' `space' firmshare

duplicates drop

gsort `space' `firm'
duplicates drop `space' `firm', force

gen counter = 1
gen temp = firmshare^2
bysort `space': egen hhi_`space' = sum(temp)
label variable hhi_`space' `"HHI index (`space')"'
drop temp
duplicates drop

bysort `space': egen firmcount = sum(counter)
*label variable firmcount "Firm 
keep `space' firmcount hhi
duplicates drop

sort `space'
merge `space' using `all'
tab _merge
drop _merge
sort `space' `instyear' `instmonth' `firm'
save `all',replace

**********************************
* HHI by time and space

capture confirm var Mon
if !_rc {

drop if `space' == ""
drop if `instyear' == .

keep `firm' `space' Mon leasecount total_count firm_space_share_time firm_space_share_time_cum `instyear' `instmonth' `instday'

duplicates drop  `firm' `space' `instyear' `instmonth' `instday', force

bysort `firm' `instyear' `instmonth' `space': egen leasecount_month = total(leasecount)
bysort `instyear' `instmonth' `space': egen total_count_month = total(total_count)

drop `instday' *share* total_count leasecount
duplicates drop

gen firm_space_share_month = leasecount_month/total_count_month

sort `space' `firm' `instyear' `instmonth'
by `space' `firm': gen signed = sum(leasecount_month)
by `space' : gen totsigned = sum(total_count_month)

gen firm_space_cumshare_month = signed/totsigned

egen opspace = group(`firm' `space')
tsset opspace Mon
tsfill , full

gsort opspace -`space'
bysort opspace: carryforward `space', replace
sort opspace `firm'
bysort opspace: carryforward `firm', replace
sort Mon `instyear'
bysort Mon: carryforward `instyear', replace
sort Mon `instmonth'
bysort Mon: carryforward `instmonth', replace

foreach var of varlist firm_space_cumshare_month firm_space_share_month {
sort `space' `firm' `instyear' `instmonth' `var'
by `space' `firm': carryforward `var', replace
}

gen temp = firm_space_share_month^2
bysort `space' `instyear' `instmonth' : egen hhi_space_time = sum(temp)
label variable hhi_space_time `"HHI by period (`space')"'

replace temp = firm_space_cumshare_month^2
bysort `space' `instyear' `instmonth' : egen hhi_space_time_cum = sum(temp)
label variable hhi_space_time `"Cum. HHI by period (`space')"'

sort `space' Mon `instyear' `instmonth'
by `space' Mon: gen opcount = _n

by `space' Mon : gen hhi_space_timeMon = hhi_space_time if opcount == 1
by `space' Mon : gen hhi_space_time_cumMon = hhi_space_time_cum if opcount == 1
sort `space' Mon opcount
by `space' Mon : carryforward hhi_space_timeMon, replace
by `space' Mon : carryforward hhi_space_time_cumMon, replace
label variable hhi_space_timeMon `"HHI by period at begin `Quant' (`space')"'

keep `space' hhi_* `instyear' `instmonth' firm_* `firm'
duplicates drop
sort `space' `instyear' `instmonth' `firm'
merge `space' `instyear' `instmonth' `firm' using `all'
tab _merge
drop if _merge == 1
drop _merge
save `all',replace
}

rename total_count `space'_day_count
rename total_sqft  `space'_day_sqft
rename leasecount  `space'_firm_day_count
rename landtotal   `space'_firm_day_sqft

rename firmshare firmshare_space
rename firmcount firmcount_space

drop counter

order `space' `wellpad' `instyear' `instmonth' `instday' `Quant'

destring `space',replace

end
